/**
 *  Copyright 2002-2009 the original author or authors.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package foo.bar.wiki.services;

import org.apache.shiro.SecurityUtils;
import org.springframework.web.servlet.ThemeResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import foo.bar.wiki.domain.User;
import foo.bar.wiki.domain.Property;

/**
 * Used by various action beans that extends off {@link foo.bar.wiki.web.action.BaseActionBean}
 * to determine the current wiki theme name.
 * <p/>
 * It resolves the theme name as follows with descending priority:-
 * <ul>
 *  <li>From session attribute named {@link #getThemeNameKey()} </li>
 *  <li>Default theme name in {@link #getDefaultThemeName()} </li>
 * </ul>
 *
 * @see {@link foo.bar.wiki.web.extension.WikiThemeInterceptor}
 *
 * @author tmjee
 * @version $Date$ $Id$
 */
public class WikiThemeResolver implements ThemeResolver {

    private String defaultThemeName = "Default";
    private String themeNameKey = "themeName" ;

    public void setDefaultThemeName(String defaultThemeName) {
        this.defaultThemeName = defaultThemeName;
    }

    public String getDefaultThemeName() {
        return defaultThemeName;
    }

    public void setThemeNameKey(String themeNameKey) {
        this.themeNameKey = themeNameKey;
    }

    public String getThemeNameKey() {
        return themeNameKey;
    }

    public String resolveThemeName(HttpServletRequest request) {
        // user theme in session if exists
        String themeName = (String) request.getSession(true).getAttribute(themeNameKey);
        if (themeName != null && themeName.trim().length() > 0) {
            return themeName.trim();
        }
        return defaultThemeName;
    }

    public void setThemeName(HttpServletRequest request, HttpServletResponse response, String themeName) {
        request.getSession(true).setAttribute(themeNameKey, themeName);
    }
}
